package models;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.data.validation.Required;
import play.db.jpa.Blob;

@Entity
public class Student extends User {

	/** Attribut désignant la formation suivi de l'étudiant **/
	@Required
	@ManyToOne
	public Formation formationStudent;

	/** Attribut désignant la liste des évaluations de l'étudiant **/
	@OneToMany(mappedBy = "studentEvaluation2", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	public Set<Evaluation2> listEvaluation2Student;

	/**
	 * Constructeur pour l'objet Student
	 * 
	 * @param lastNameUser
	 * @param firstNameUser
	 * @param emailUser
	 * @param passwordUser
	 * @param avatarPathUser
	 * @param formationStudent
	 */
	public Student(String lastNameUser, String firstNameUser, String emailUser,
			String passwordUser, Blob avatarPathUser, Formation formationStudent) {
		super(lastNameUser, firstNameUser, emailUser, passwordUser);
		this.formationStudent = formationStudent;
		this.listEvaluation2Student = new HashSet<Evaluation2>();
	}

	/**
	 * Constructeur à partir d'un user existant
	 * 
	 * @param userToCopy
	 */
	public Student(User userToCopy, Formation formation,
			Set<Evaluation2> evaluation2s) {
		this(userToCopy.lastNameUser, userToCopy.firstNameUser,
				userToCopy.emailUser, userToCopy.passwordUser,
				userToCopy.avatarPathUser, formation);
		this.id = userToCopy.id;
		this.listEvaluation2Student = evaluation2s;
	}

	/**
	 * Constructeur vide pour l'objet Student
	 */
	public Student() {
		super(null, null, null, null);
	}

	/**
	 * Méthode permettant d'authentifier un étudiant
	 * 
	 * @param emailUser
	 * @param passwordUser
	 * @return l'étudiant récupéré de la base de donnée
	 */
	public static Student connect(String emailUser, String passwordUser) {
		return find("byEmailUserAndPasswordUser", emailUser, passwordUser)
				.first();
	}

	public static Student create(Student student) {
		return student.create() ? student : null;
	}

	public void matchWithOtherUser(Student studentToCompare) {
		super.matchWithOtherUser(new Enseignant(studentToCompare.lastNameUser,
				studentToCompare.firstNameUser, studentToCompare.emailUser, studentToCompare.passwordUser,
				studentToCompare.avatarPathUser));
		if (this.formationStudent == null ) 
			this.formationStudent = studentToCompare.formationStudent ;
		
		this.listEvaluation2Student = studentToCompare.listEvaluation2Student;
	}

}
